package com.it.gmall.realtime.util;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.util.Properties;

/**
 * @author : code1997
 * @date :2021-01-2021/1/28 21:03
 */
public class RedisUtil {

    private static JedisPool jedisPool = null;


    public static Jedis getJedisClient() {
        if (jedisPool == null) {
            synchronized (RedisUtil.class) {
                if (jedisPool == null) {
                    Properties properties = PropertiesUtil.load("redis.properties");
                    String host = String.valueOf(properties.get("redis.host"));
                    int port = Integer.parseInt(String.valueOf(properties.get("redis.port")));
                    JedisPoolConfig config = new JedisPoolConfig();
                    config.setMaxTotal(100);
                    config.setMaxIdle(200);
                    config.setMinIdle(20);
                    config.setBlockWhenExhausted(true);
                    config.setMaxWaitMillis(5000);
                    config.setTestOnBorrow(true);
                    jedisPool = new JedisPool(config, host, port);
                }
            }
        }
        return jedisPool.getResource();
    }

    public static void main(String[] args) {
        Jedis jedisClient = RedisUtil.getJedisClient();
        String ping = jedisClient.get("ping");
        System.out.println(ping);
    }

}
